package com.example.sunmiv1.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.example.sunmiv1.R;
import com.example.sunmiv1.activity.StringCodeOperationActivity;
import com.example.sunmiv1.entity.AllocationDetailInfo;
import com.example.sunmiv1.utils.net.ACache;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AllocationDetailAdapter extends BaseAdapter {

    private Context context;
    private List<AllocationDetailInfo.DataBean> list;
    private ViewHolder viewHolder;
    private int type;
    private String pricePermission;// 門店查看價格權限

    public AllocationDetailAdapter(Context context, List<AllocationDetailInfo.DataBean> list, int type) {
        this.context = context;
        this.list = list;
        this.type = type;
        ACache aCache = ACache.get(context);
        pricePermission = aCache.getAsString("showPrice");
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.deposit_detail_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final int index = position;
        AllocationDetailInfo.DataBean dataBean = list.get(position);
        if (type == 1) {//1.AllocationAdapter入庫詳情 2.AllocationAdapter出庫詳情 3.PurchaseOrderActivity門店採購單詳情 4.PurchaseOrderActivity商戶採購單詳情
            viewHolder.depositDetailLin.setVisibility(View.VISIBLE);
            viewHolder.PurchaseDetailLin.setVisibility(View.GONE);
            viewHolder.depositDetailName.setText(dataBean.getGoods_name());
            if ("2".equals(list.get(position).getAttribute_state())) {
                viewHolder.depositDetailSkuNameLin.setVisibility(View.VISIBLE);
                viewHolder.depositDetailSkuName.setText(list.get(position).getSku_name());
            } else {
                viewHolder.depositDetailSkuNameLin.setVisibility(View.GONE);
            }
            viewHolder.depositDetailCode.setText(dataBean.getGoods_code());
            viewHolder.depositDetailFreightNumber.setText(dataBean.getGoods_number());
            viewHolder.depositDetailSkuNum.setText(dataBean.getSku_num() + "");
            viewHolder.depositDetailNum.setText(dataBean.getStock_num() + "");
            viewHolder.depositDetailPrice.setText(dataBean.getStock_price() + "");
            viewHolder.depositDetailTotal.setText(dataBean.getSubtotal() + "");
            if (TextUtils.isEmpty(dataBean.getRemarks())) {
                viewHolder.groupRemark1.setVisibility(View.GONE);
            } else {
                viewHolder.groupRemark1.setVisibility(View.VISIBLE);
                viewHolder.infoRemark1.setText(dataBean.getRemarks());
            }
            if ("1".equals(dataBean.getSerial_number_state())) {
                viewHolder.depositDetailNum.setTextColor(ContextCompat.getColor(context, R.color.blue_3293FA));
            } else {
                viewHolder.depositDetailNum.setTextColor(ContextCompat.getColor(context, R.color.text_666));
            }
            viewHolder.depositDetailNum.setOnClickListener(v -> {
                if ("1".equals(list.get(index).getSerial_number_state())) {
                    Intent intent = new Intent(context, StringCodeOperationActivity.class);
                    intent.putExtra("operation", 10);
                    intent.putExtra("id", list.get(index).getId());
                    context.startActivity(intent);
                }
            });
        } else if (type == 2) {
            viewHolder.depositDetailLin.setVisibility(View.VISIBLE);
            viewHolder.PurchaseDetailLin.setVisibility(View.GONE);
            viewHolder.depositDetailName.setText(dataBean.getGoods_name());
            if ("2".equals(list.get(position).getAttribute_state())) {
                viewHolder.depositDetailSkuNameLin.setVisibility(View.VISIBLE);
                viewHolder.depositDetailSkuName.setText(list.get(position).getSku_name());
            } else {
                viewHolder.depositDetailSkuNameLin.setVisibility(View.GONE);
            }
            viewHolder.depositDetailCode.setText(dataBean.getGoods_code());
            viewHolder.depositDetailFreightNumber.setText(dataBean.getGoods_number());
            viewHolder.depositDetailSkuNum.setText(dataBean.getSku_num() + "");
            viewHolder.depositDetailNum.setText(dataBean.getWarehouse_num() + "");
            viewHolder.depositDetailPrice.setText(dataBean.getWarehouse_price() + "");
            viewHolder.depositDetailTotal.setText(dataBean.getSubtotal() + "");
            if (TextUtils.isEmpty(dataBean.getRemarks())) {
                viewHolder.groupRemark1.setVisibility(View.GONE);
            } else {
                viewHolder.groupRemark1.setVisibility(View.VISIBLE);
                viewHolder.infoRemark1.setText(dataBean.getRemarks());
            }
            if ("1".equals(dataBean.getSerial_number_state())) {
                viewHolder.depositDetailNum.setTextColor(ContextCompat.getColor(context, R.color.blue_3293FA));
            } else {
                viewHolder.depositDetailNum.setTextColor(ContextCompat.getColor(context, R.color.text_666));
            }
            viewHolder.depositDetailNum.setOnClickListener(v -> {
                if ("1".equals(list.get(index).getSerial_number_state())) {
                    Intent intent = new Intent(context, StringCodeOperationActivity.class);
                    intent.putExtra("operation", 10);
                    intent.putExtra("id", list.get(index).getId());
                    context.startActivity(intent);
                }
            });
        } else if (type == 3 || type == 4) {
            viewHolder.depositDetailLin.setVisibility(View.GONE);
            viewHolder.PurchaseDetailLin.setVisibility(View.VISIBLE);
            viewHolder.purchaseDetailName.setText(dataBean.getGoods_name());
            if (TextUtils.isEmpty(dataBean.getSku_name())) {
                viewHolder.purchaseDetailSkuLin.setVisibility(View.GONE);
            } else {
                viewHolder.purchaseDetailSkuLin.setVisibility(View.VISIBLE);
                viewHolder.purchaseDetailSkuName.setText(dataBean.getSku_name());
            }
            viewHolder.purchaseDetailCode.setText(dataBean.getGoods_code());
            viewHolder.purchaseDetailFreightNumber.setText(dataBean.getGoods_number());
            viewHolder.purchaseDetailPrice.setText(dataBean.getPurchase_price() + "");
            viewHolder.purchaseDetailNum.setText(dataBean.getPurchase_num() + "");
            viewHolder.purchaseDetailTotal.setText(dataBean.getSubtotal() + "");
            viewHolder.depositDetailRetailPrice.setText(dataBean.getRetail_price());
            viewHolder.depositDetailOnlinePrice.setText(dataBean.getOnline_price());
            if (TextUtils.isEmpty(dataBean.getRemarks())) {
                viewHolder.groupRemark2.setVisibility(View.GONE);
            } else {
                viewHolder.groupRemark2.setVisibility(View.VISIBLE);
                viewHolder.infoRemark2.setText(dataBean.getRemarks());
            }
            viewHolder.purchaseDetailRetailPrice.setText(dataBean.getRetail_price());
            viewHolder.purchaseDetailOnlinePrice.setText(dataBean.getOnline_price());
            viewHolder.purchaseDetailBuildDate.setText(dataBean.getBuild_date());
            viewHolder.purchaseDetailQualityGuaranteePeriod.setText(dataBean.getQuality_guarantee_period());
            if ("1".equals(dataBean.getSerial_number_state())) {
                viewHolder.purchaseDetailNum.setTextColor(ContextCompat.getColor(context, R.color.blue_3293FA));
            } else {
                viewHolder.purchaseDetailNum.setTextColor(ContextCompat.getColor(context, R.color.text_666));
            }
            viewHolder.purchaseDetailNum.setOnClickListener(v -> {
                if ("1".equals(list.get(index).getSerial_number_state())) {
                    Intent intent = new Intent(context, StringCodeOperationActivity.class);
                    intent.putExtra("operation", 10);
                    intent.putExtra("id", list.get(index).getId());
                    context.startActivity(intent);
                }
            });
        }
        if ("2".equals(pricePermission)) {
            ((View) viewHolder.depositDetailPriceMsg.getParent()).setVisibility(View.GONE);
            ((View) viewHolder.depositDetailTotal.getParent()).setVisibility(View.GONE);
            ((View) viewHolder.purchaseDetailPrice.getParent()).setVisibility(View.GONE);
            ((View) viewHolder.purchaseDetailTotal.getParent()).setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(list.get(position).getCompany())) {
            ((View) viewHolder.depositDetailUnit.getParent()).setVisibility(View.GONE);
        } else {
            ((View) viewHolder.depositDetailUnit.getParent()).setVisibility(View.VISIBLE);
            viewHolder.depositDetailUnit.setText(dataBean.getCompany());
        }
        if (TextUtils.isEmpty(list.get(position).getCompany())) {
            ((View) viewHolder.purchaseDetailUnit.getParent()).setVisibility(View.GONE);
        } else {
            ((View) viewHolder.purchaseDetailUnit.getParent()).setVisibility(View.VISIBLE);
            viewHolder.purchaseDetailUnit.setText(dataBean.getCompany());
        }
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.depositDetail_name)
        TextView depositDetailName;
        @BindView(R.id.depositDetail_skuName)
        TextView depositDetailSkuName;
        @BindView(R.id.depositDetail_skuNameLin)
        LinearLayout depositDetailSkuNameLin;
        @BindView(R.id.depositDetail_code)
        TextView depositDetailCode;
        @BindView(R.id.depositDetail_freightNumber)
        TextView depositDetailFreightNumber;
        @BindView(R.id.depositDetail_unit)
        TextView depositDetailUnit;
        @BindView(R.id.depositDetail_build_date)
        TextView depositDetailBuildDate;
        @BindView(R.id.depositDetail_quality_guarantee_period)
        TextView depositDetailQualityGuaranteePeriod;
        @BindView(R.id.depositDetail_skuNum)
        TextView depositDetailSkuNum;
        @BindView(R.id.depositDetail_numMsg)
        TextView depositDetailNumMsg;
        @BindView(R.id.depositDetail_num)
        TextView depositDetailNum;
        @BindView(R.id.depositDetail_priceMsg)
        TextView depositDetailPriceMsg;
        @BindView(R.id.depositDetail_price)
        TextView depositDetailPrice;
        @BindView(R.id.depositDetail_total)
        TextView depositDetailTotal;
        @BindView(R.id.info_remark_1)
        TextView infoRemark1;
        @BindView(R.id.group_remark_1)
        LinearLayout groupRemark1;
        @BindView(R.id.depositDetail_lin)
        LinearLayout depositDetailLin;
        @BindView(R.id.purchaseDetail_name)
        TextView purchaseDetailName;
        @BindView(R.id.purchaseDetail_skuName)
        TextView purchaseDetailSkuName;
        @BindView(R.id.purchaseDetail_skuLin)
        LinearLayout purchaseDetailSkuLin;
        @BindView(R.id.purchaseDetail_code)
        TextView purchaseDetailCode;
        @BindView(R.id.purchaseDetail_freightNumber)
        TextView purchaseDetailFreightNumber;
        @BindView(R.id.purchaseDetail_unit)
        TextView purchaseDetailUnit;
        @BindView(R.id.purchaseDetail_build_date)
        TextView purchaseDetailBuildDate;
        @BindView(R.id.purchaseDetail_quality_guarantee_period)
        TextView purchaseDetailQualityGuaranteePeriod;
        @BindView(R.id.purchaseDetail_price)
        TextView purchaseDetailPrice;
        @BindView(R.id.purchaseDetail_num)
        TextView purchaseDetailNum;
        @BindView(R.id.purchaseDetail_total)
        TextView purchaseDetailTotal;
        @BindView(R.id.info_remark_2)
        TextView infoRemark2;
        @BindView(R.id.group_remark_2)
        LinearLayout groupRemark2;
        @BindView(R.id.PurchaseDetail_lin)
        LinearLayout PurchaseDetailLin;
        @BindView(R.id.depositDetail_retailPrice)
        TextView depositDetailRetailPrice;
        @BindView(R.id.depositDetail_onlinePrice)
        TextView depositDetailOnlinePrice;
        @BindView(R.id.purchaseDetail_retailPrice)
        TextView purchaseDetailRetailPrice;
        @BindView(R.id.purchaseDetail_onlinePrice)
        TextView purchaseDetailOnlinePrice;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
